﻿using ToolKitIESiteList.Library;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace ToolKitManagerIESiteList
{
    public partial class FrmMerge : Form
    {
        private bool Resizing;
        private List<IEModeDataItem> OVwDel = new List<IEModeDataItem>();

        public ResultMerge DataMeger { get; set; }
        public string FileName { get; set; }
        public string TitleWindows { get; set; }
        public ImportOption ShowOption { get; set; }


        public FrmMerge()
        {
            InitializeComponent();
        }

        private void FrmMerge_Load(object sender, EventArgs e)
        {
            ListView_SizeChanged(listViewDataUrl, e);
        }

        private void ListView_SizeChanged(object sender, EventArgs e)
        {
            // Don't allow overlapping of SizeChanged calls
            if (!Resizing)
            {
                // Set the resizing flag
                Resizing = true;

                ListView listView = sender as ListView;
                if (listView != null)
                {
                    float totalColumnWidth = 0;

                    // Get the sum of all column tags
                    for (int i = 0; i < listView.Columns.Count; i++)
                        totalColumnWidth += Convert.ToInt32(listView.Columns[i].Tag);

                    // Calculate the percentage of space each column should 
                    // occupy in reference to the other columns and then set the 
                    // width of the column to that percentage of the visible space.
                    for (int i = 0; i < listView.Columns.Count; i++)
                    {
                        float colPercentage = (Convert.ToInt32(listView.Columns[i].Tag) / totalColumnWidth);
                        listView.Columns[i].Width = (int)(colPercentage * listView.ClientRectangle.Width);
                    }
                }
            }
            // Clear the resizing flag
            Resizing = false;
        }

        private void BntClose_Click(object sender, EventArgs e)
        {
            if (OVwDel.Count > 0)
            {
                if (TitleWindows == Properties.Resources.OpenSiteList)
                {
                    ManagerHotTracker.AddTraker(ActionTracker.OpenFile, Properties.Resources.MergeOverwirte, UCFileName.TextLabel, OVwDel);
                }
                else if (TitleWindows == Properties.Resources.MergeSiteList)
                {
                    ManagerHotTracker.AddTraker(ActionTracker.MergeFile, Properties.Resources.MergeOverwirte, UCFileName.TextLabel, OVwDel);
                }
                else if (TitleWindows == Properties.Resources.MergeFromSiteDiscovery)
                {
                    ManagerHotTracker.AddTraker(ActionTracker.ImportSiteDiscovery, Properties.Resources.MergeOverwirte, UCFileName.TextLabel, OVwDel);
                }
                else
                {
                    ManagerHotTracker.AddTraker(ActionTracker.Unknown, Properties.Resources.MergeOverwirte, UCFileName.TextLabel, OVwDel);
                }
            }
            var skip = new List<IEModeDataItem>();
            foreach (ListViewItem item in listViewDataUrl.Items)
            {
                if (!item.Checked)
                {
                    var data = (IEModeDataItem)item.Tag;
                    if (!OVwDel.Contains(data))
                    {
                            skip.Add(data);
                    }
                }
            }
            if (skip.Count > 0)
            {
                if (TitleWindows == Properties.Resources.MergeSiteList)
                {
                    ManagerHotTracker.AddTraker(ActionTracker.MergeFile, Properties.Resources.SkipMergeOverwirte, UCFileName.TextLabel, skip);
                }
                else if (TitleWindows == Properties.Resources.MergeFromSiteDiscovery)
                {
                    ManagerHotTracker.AddTraker(ActionTracker.ImportSiteDiscovery, Properties.Resources.SkipMergeOverwirte, UCFileName.TextLabel, skip);
                }
                else
                {
                    ManagerHotTracker.AddTraker(ActionTracker.Unknown, Properties.Resources.SkipMergeOverwirte, UCFileName.TextLabel, skip);
                }
            }
            Close();
        }

        private void FrmMerge_Shown(object sender, EventArgs e)
        {
            this.Text = TitleWindows;
            UCFileName.TextLabel = FileName;
            BntOverWrite.Enabled = DataMeger.Duplicates.Length > 0;
            UCTotNewUrl.TextLabel = DataMeger.NewItems.Length.ToString();
            UCTotDupUrl.TextLabel = DataMeger.Duplicates.Count(s => s.IsLogicalDuplicate).ToString();

            var ovw = DataMeger.Duplicates.Count(s => !s.IsLogicalDuplicate);
            UCTotDupOvw.TextLabel = ovw.ToString();
            if (ovw > 0)
            {
                ShowOption = ImportOption.Overwrite;
            }
            switch (ShowOption)
            {
                case ImportOption.New:
                    {
                        OptNew.Checked = true;
                        OptNew_Click(null, new EventArgs());
                        break;
                    }
                case ImportOption.Duplicate:
                    {
                        OptDup.Checked = true;
                        OptDup_Click(null, new EventArgs());
                        break;
                    }
                case ImportOption.Overwrite:
                    {
                        OptOvw.Checked = true;
                        OptOvw_Click(null, new EventArgs());
                        break;
                    }
            }
        }

        private void AddItem(IEModeDataItem item)
        {
            ListViewItem lvi = new ListViewItem(item.Url);
            lvi.SubItems.Add(item.CompatMode.ToString()); 
            lvi.SubItems.Add(item.OpenIn.ToString());
            lvi.SubItems.Add(item.OriCompatMode.ToString());
            lvi.SubItems.Add(item.OriOpenIn.ToString());
            lvi.Tag = item;
            if (OVwDel.Contains(item))
            {
                lvi.Font = new Font(lvi.Font, FontStyle.Strikeout);
                lvi.ForeColor = SystemColors.GrayText;
            }
            lvi.SubItems[1].ForeColor = Color.Red;
            lvi.SubItems[2].ForeColor = Color.Red;
            lvi.UseItemStyleForSubItems = false;
            listViewDataUrl.Items.Add(lvi);
        }

        private void OptDup_Click(object sender, EventArgs e)
        {
            listViewDataUrl.SuspendLayout();
            listViewDataUrl.Items.Clear();
            listViewDataUrl.CheckBoxes = false;
            BntOverWrite.Enabled = false;
            ChkAll.Enabled = false;
            ChkAll.Checked = false;
            foreach (var item in DataMeger.Duplicates.Where(s => s.IsLogicalDuplicate))
            {
                AddItem(item);
            }
            listViewDataUrl.ResumeLayout();
        }

        private void OptNew_Click(object sender, EventArgs e)
        {
            listViewDataUrl.SuspendLayout();
            listViewDataUrl.Items.Clear();
            listViewDataUrl.CheckBoxes = false;
            BntOverWrite.Enabled = false;
            ChkAll.Enabled = false;
            ChkAll.Checked = false;
            foreach (var item in DataMeger.NewItems)
            {
                AddItem(item);
            }
            listViewDataUrl.ResumeLayout();
        }

        private void OptOvw_Click(object sender, EventArgs e)
        {
            listViewDataUrl.SuspendLayout();
            listViewDataUrl.Items.Clear();
            listViewDataUrl.CheckBoxes = true;
            ChkAll.Checked = false;
            ChkAll.Checked = false;
            foreach (var item in DataMeger.Duplicates.Where(s => !s.IsLogicalDuplicate))
            {
                AddItem(item);
            }
            BntOverWrite.Enabled = listViewDataUrl.Items.Count > 0;
            ChkAll.Enabled = BntOverWrite.Enabled;
            listViewDataUrl.ResumeLayout();
        }

        private void ChkAll_Click(object sender, EventArgs e)
        {
            if (!listViewDataUrl.CheckBoxes) return;
            listViewDataUrl.SuspendLayout();
            foreach (ListViewItem item in listViewDataUrl.Items)
            {
                item.Checked = ChkAll.Checked;
            }
            listViewDataUrl.ResumeLayout();
        }

        private void BntOverWrite_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listViewDataUrl.Items)
            {
                if (item.Checked)
                {
                    var data = (IEModeDataItem)item.Tag;
                    OVwDel.Add(data);
                    EmieDocument.OverWriterCompatMode(data.CompatMode,data.Url);
                    EmieDocument.OverWriterOpenIn(data.OpenIn, data.Url);
                    item.Font =  new Font(item.Font, FontStyle.Strikeout);
                    item.ForeColor = SystemColors.GrayText;
                    item.Checked = false;
                }
            }
            if (OVwDel.Count == listViewDataUrl.Items.Count)
            {
                ChkAll.Enabled = false;
                ChkAll.Checked = false;
                BntOverWrite.Enabled = false;
            }
        }

        private void listViewDataUrl_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            var data = (IEModeDataItem)listViewDataUrl.Items[e.Index].Tag;
            if (OVwDel.Contains(data))
            {
                e.NewValue = CheckState.Unchecked;
            }
        }
    }
}
